﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.IO;

using XNET.Extension;
using XNET.SQL;
using XNET.WEB;
using XNET.IO;
using XNET.Core;
using XNET.Email;
using XNET.GDI;
using System.Drawing;

namespace XNETTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("完成");
            Console.ReadLine();
        }

        #region SQL
        //插入
        private static void InsertBook()
        {
            string strSql="insert into books(name,author,price,memo) values(@name,@author,@price,@memo)";

            DatabaseFactory.CreateDatabase("sqlite").ExecuteNonQuery(strSql, new { name = "C#入门", author = "C#", price = 15.2, memo = "经典书籍" });
        }
        //更新
        private static void UpdateBook()
        {
            string strSql = "update books set name=@name,author=@author where ID=@id";

            DatabaseFactory.CreateDatabase("sqlite").ExecuteNonQuery(strSql, new { name = "WCF", author = "WCF作者", id = 1 });
        }
        //删除
        private static void DeleteBook()
        {
            string strSql = "delete from books where ID=@id";

            DatabaseFactory.CreateDatabase("sqlite").ExecuteNonQuery(strSql, new { id = 1 });
        }
        //获取单值
        private static int GetSingle()
        {
            string strSql = "select count(1) from books";
            return DatabaseFactory.CreateDatabase("sqlite").ExecuteScalar<int>(strSql, null);
        }
        //执行事务
        public static void ExecTran()
        {
            IDatabase db = DatabaseFactory.CreateDatabase("sqlite");

            db.ExecuteTransaction((tx) =>
            {
                var orderId = tx.ExecuteDataReader(@"insert into [orders](custid, trndate) values(@custid, @trndate); select SCOPE_IDENTITY()",
                    new { custid = 1, trndate = DateTime.Now }, dr => Convert.ToInt32(dr[0]))
                    .FirstOrDefault();

                tx.ExecuteNonQuery("insert into orderdetail(qty, price, bid,amount,oid) values(@qty, @price, @bid,@money,@oid)",
                    new { qty = 2, price = 15, bid = 1, amount = 30, oid = orderId });
            });
        }
        //获取记录
        public static List<BookData> GetList()
        {
            IDatabase db = DatabaseFactory.CreateDatabase("sqlite");

            return db.ExecuteDataReader<BookData>("select ID,name,author,memo from books", null, dr =>
                {
                    return new BookData
                    {
                        ID = dr["ID"].GetValue<int>(0),
                        Name = dr["name"].GetValue<string>(string.Empty),
                        Author = dr["Author"].GetValue<string>(string.Empty),
                        Memo = dr["memo"].GetValue<string>(string.Empty)
                    };
                }).ToList();
        }
        //获取记录
        public static List<BookData> GetList1()
        {
            IDatabase db = DatabaseFactory.CreateDatabase("sqlite");

            List<BookData> books = new List<BookData>();
            db.ExecuteDataReader("select ID,name,author,memo from books", null, dr =>
                {
                    BookData book = new BookData
                    {
                        ID = dr["ID"].GetValue<int>(0),
                        Name = dr["name"].GetValue<string>(string.Empty),
                        Author = dr["Author"].GetValue<string>(string.Empty),
                        Memo = dr["memo"].GetValue<string>(string.Empty)
                    };
                    books.Add(book);
                });
            return books;
        }
        #endregion

        #region WEB
        private static void GetLocalIPAddr()
        {
            IPAddress[] items = XNet.GetLocalIPAddr();
            foreach (var item in items)
            {
                Console.WriteLine(item.ToString());
            }
        }
        #endregion

        #region IO
        /// <summary>
        /// 测试数字签名
        /// </summary>
        public static void TestSign()
        {
            string originalData = "文章不错，这是我的签名：奥巴马！";
            Console.WriteLine("签名数为：{0}", originalData);
            KeyValuePair<string, string> keyPair = XEncrypt.CreateRSAKey();
            string privateKey = keyPair.Value;
            string publicKey = keyPair.Key;

            //1、生成签名，通过摘要算法
            string signedData = XEncrypt.HashAndSignString(originalData, privateKey);
            Console.WriteLine("数字签名:{0}", signedData);

            //2、验证签名
            bool verify = XEncrypt.VerifySigned(originalData, signedData, publicKey);
            Console.WriteLine("签名验证结果：{0}", verify);
        }
        #endregion

        #region GDI
        private static void Suoxiao()
        {
            string file = @"G:\ARE107.jpg";
            //Bitmap bmp = new Bitmap(file);
            //FileStream fs = new FileStream(file, FileMode.Open);
            XImage.ImageCompress(file, @"G:\ARE107-Y.jpg", 900, 500, 100);
            //XImage.ImageZoomAuto(fs, @"G:\ARE107-A.png", 900, 500, "", "");
        }
        #endregion
    }

    public class BookData
    {
        public int ID { get; set; }

        public string Name { get; set; }

        public string Author { get; set; }

        public string Memo { get; set; }
    }
}
